package org.fluidripple.socket.protocol;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.fluidripple.socket.operation.AutomationOperation;
import org.fluidripple.socket.operation.ChannelOperation;
import org.fluidripple.socket.session.Session;
import org.fluidripple.socket.session.SessionManager;
import org.fluidripple.socket.stickhalf.FixedLengthDecoderImpl;
import org.fluidripple.socket.wrapper.DataWrapper;
import org.springframework.stereotype.Component;

/**
 * 停止采集
 */
@Slf4j
@Component
public class DeviceStopCollectProtocol extends Protocol{
    @Override
    public void writer(Channel channel) {
        Session session = SessionManager.getSession(channel);
        session.setProtocol(this);
        ChannelOperation.removeChannelHandler(channel, FixedLengthDecoderImpl.name);
        for (int i = 0; i <15 ; i++){
            ByteBuf buf = Unpooled.buffer();
            buf.writeBytes(ByteBufUtil.decodeHexDump("F0C103A1A4"));
            channel.writeAndFlush(buf);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public void toDecode(DataWrapper dataWrapper) {
        Session session = dataWrapper.getSession();
        session.setCollectData(false);
        Channel channel = session.getChannel();
        AutomationOperation.addDelayQueue(channel);
    }
}
